package uns.fit.gis.alg;

public class Queue { 
    Object[] arr;
    int      putPos;
    int      getPos;
    int      nElems;

    public Queue() { 
        arr = new Object[1024];
    }

    public int size() { 
        return nElems;
    }

    public Object dequeue() { 
        Object obj = arr[getPos];
        getPos = ++getPos & (arr.length-1);
        nElems -= 1;
        return obj;
    }

    public void enqueue(Object obj) { 
        arr[putPos] = obj;
        putPos = ++putPos & (arr.length-1);
        nElems += 1;
        if (putPos == getPos) { 
            Object[] newArr = new Object[arr.length*2];
            System.arraycopy(arr, 0, newArr, 0, putPos);
            System.arraycopy(arr, getPos, newArr, arr.length + getPos, arr.length - getPos);
            getPos += arr.length;
            arr = newArr;
        }
    }
}